import csv

import torch

from eval import show_without_plt


def csv_creater(file, fieldnames):
    writer = csv.DictWriter(file, fieldnames=fieldnames)
    writer.writeheader()
    file.flush()
    return writer

def save_log(epoch, input, orthogonal_error, file,writer,model_R,csv_name):
    global fieldnames
    print("------------------------------------")
    print(f"epoch: {epoch}", end='')
    result = show_without_plt(input, print_info=True)
    print(f"orthogonal_error: {orthogonal_error[0]}")
    print(f"det: {orthogonal_error[1]}")

    result["orthogonal_error"] = orthogonal_error[0]
    result["det"] = orthogonal_error[1]
    writer.writerow(result)
    # save_csv = file.write(','.join([str(i) for i in arr]) + '\n')
    torch.save(model_R, f'{csv_name}.pth')
    file.flush()
    print("------------------------------------")